// pages/newRegister/newRegister.js
var app = getApp();
var ea = require('../../utils/each.js');
var time = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {

    //多选
    items: [

    ],


    getxiaoquUrl: app.data.url + 'zhongxue/teacher/getxiaoqu',
    getxueduanUrl: app.data.url + 'zhongxue/teacher/getxueduan',
    getkemuUrl: app.data.url + 'zhongxue/teacher/getkemu',
    getzhiweiUrl: app.data.url + 'zhongxue/teacher/getzhiwei',
    getmyUrl: app.data.url + 'zhongxue/teacher/getmy',
    getxiugaiUrl: app.data.url + 'zhongxue/teacher/xiugaimy',

    // mUrl:[
    //   "/img/f24e9741b220628c727c227b4cc6c2d.png"
    // ],

    newRegisterUrl: [
      "/img/8a27a18bf612eadcb95729392a9ffe6.png"
    ],

    newUrl: [
      "/img/19.png"
    ],
    upUrl: [
      "/img/9fcdbf45551d9eaa18ca4d8c9a13b84.png"
    ],
    //选择男女
    equlity: [
      { id: 0, names: "男", checked: 'true' },
      { id: 1, names: "女" },
    ],
    //学历
    xueli: [
      { id: 0, name: "初中" },
      { id: 1, name: "高中" },
      { id: 2, name: "大学" },
      { id: 3, name: "研究生" },
      { id: 4, name: "博士生" },
      { id: 5, name: "硕士生" },
      { id: 6, name: "其他" }
    ],
    index: "",
    //职位
    zhiwei: [],
    index1: "",

    //学段
    xueduan: [],
    index2: "",

    //科目
    kemu: [],
    index3: "",

    //校区
    campus_id: [],
    index4: "",

    //生日
    nianlin: "",

    //上岗时间
    shangg: "",

    shuju: { },
  },
  newRegister: function () {
    wx.reLaunch({
      url: '/pages/index/index',
    })
  },

  //获取input框所有内容
  inputWacth: function (e) {
    // console.log(e);
    let item = e.currentTarget.dataset.model;
    // console.log(item);
    var shuju = this.data.shuju;
    shuju[item] = e.detail.value;
    console.log(shuju[item])
    this.setData({ shuju: shuju });
    this.setData({
      [item]: e.detail.value
    });
    console.log(shuju)

  },

  // 选择男女
  changebc: function (e) {
    // console.log(e);
    var index = e.currentTarget.dataset.index;
    // console.log(index)
    // console.log(this)
    var shujus = this.data.equlity;
    var shuju = this.data.shuju;
    // console.log(shuju);
    ea.each(shujus, function (i, v) {
      if (i == index) {
        shuju.sex = v.names;
      }
    })
    this.setData({ shuju: shuju });
    // console.log(shuju);

  },


  //学历
  xueli: function (e) {
    // console.log(e);
    var index = e.detail.value;
    // console.log(index)
    var that = this;
    var xueli = this.data.xueli;
    var shuju = this.data.shuju;
    ea.each(xueli, function (i, v) {
      if (i == index) {
        that.setData({ time: v });
        shuju.xueli = v.name;
        // console.log(shuju);
        that.setData({ shuju: shuju })
      }
    })
    // console.log(shuju)
  },

  //职位
  zhiwei: function (e) {
    // console.log(e);
    var index = e.detail.value;
    // console.log(index)
    var that = this;
    var zhiwei = this.data.zhiwei;
    var shuju = this.data.shuju;
    ea.each(zhiwei, function (i, v) {
      if (i == index) {
        that.setData({ time: v });
        shuju.zhiwei = v.name;
        // console.log(shuju);
        that.setData({ shuju: shuju })
      }
    })
    // console.log(shuju)
  },

  //学段
  xueduan: function (e) {
    // console.log(e);
    var index = e.detail.value;
    // console.log(index)
    var that = this;
    var xueduan = this.data.xueduan;
    var shuju = this.data.shuju;
    ea.each(xueduan, function (i, v) {
      if (i == index) {
        that.setData({ time: v });
        shuju.xueduan = v.name;
        // console.log(shuju);
        that.setData({ shuju: shuju })
      }
    })
    console.log(shuju)
  },

  //科目
  kemu: function (e) {
    var index = e.detail.value;
    // console.log(index)
    var that = this;
    var kemu = this.data.kemu;
    var shuju = this.data.shuju;
    ea.each(kemu, function (i, v) {
      if (i == index) {
        that.setData({ time: v });
        shuju.kemu = v.name;
        // console.log(shuju);
        that.setData({ shuju: shuju })
      }
    })
    // console.log(shuju)  
  },

  //校区
  checkboxChange: function (e) {
    // console.log(e);
    var arr = e.detail.value;
    // console.log(campus_id)
    var that = this;
    // console.log(that);
    var shuju = this.data.shuju;
    var campus_id = arr.join(',');
    console.log(campus_id)
    shuju.campus_id = campus_id;
    console.log(shuju)
  },

  //年龄
  shengri: function (e) {
    // console.log(e);
    var that = this;
    var shengri = e.detail.value;
    var shuju = this.data.shuju;
    // console.log(shuju);
    ea.each(shengri, function (i, v) {
      shuju.shengri = shengri;
      // console.log(shuju);
      that.setData({ shuju: shuju })
    })
    console.log(shuju)
  },

  //时间
  renzhi_time: function (e) {
    // console.log(e);
    var that = this;
    var renzhi_time = e.detail.value;
    var shuju = this.data.shuju;
    // console.log(shuju);
    ea.each(renzhi_time, function (i, v) {
      shuju.renzhi_time = renzhi_time;
      // console.log(shuju);
      that.setData({ shuju: shuju })
    })
  },

  // //上传文件
  // chooseImg: function (e) {
  //   wx.chooseImage({
  //     success: function(res) {},
  //   })
  // },
  //预览图片
  // previewImg:function(e){
  //   //获取图片下标
  //   // console.log(e)
  //   var index=e.currentTarget.dataset.index;
  //   //获取所有图片；
  //   var imgs=this.data.imgs;
  //   wx.previewImage({
  //     current:imgs[index],
  //     urls:imgs,
  //   })
  // },

  /**
   * 提交修改
   */
  xiugai: function () {
    var that = this;
    var shuju = this.data.shuju;
    // console.log(shuju);
    var yanz = ea.yanzhen(shuju, {
      personal: '请输入个人简介',
      campus_id: '请选择你任职的校区',
      renzhi_time: '请选择你任职的时间',
      kemu: '请选择你任职的科目',
      xueduan: '请选择你任职的学段',
      zhiwei: '请选择你的职位',
      zhicheng: '请填写你的职称',
      xuexiao: '请填写你的毕业学校',
      xueli: '请选择你的最高学历',
      email: '请填写你的邮箱',
      phone: '请填写你的联系电话',
      address: '请填写你的家庭住址',
      sex: '请选择你的性别',
      shengri: '请选择你的生日',
      name: '请填写你的姓名',
    })
    if (yanz) {
      wx.showToast({
        title: yanz,
        icon: 'none'
      })
      return;
    }
    var user = wx.getStorageSync('newuser')
    // console.log(user);
    shuju.token = user.token;
    // console.log(shuju);
    // return
    wx.request({
      url: that.data.getxiugaiUrl,
      method: 'post',
      data: shuju,
      success: function (res) {
        var data = res.data;
        console.log(data)
        // return
        if (data.code == 200) {
          wx.showToast({
            title: '修改成功',
            icon: 'success',
          })
          setTimeout(function () {
            wx.navigateBack({
              delta: 1,
            })
          }, 1000)
        } else {
          wx.showToast({
            title: '修改失败',
            icon: 'none',
          })
        }
      }
    })
  },





  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // console.log(that);
    var user = wx.getStorageSync('newuser')
    console.log(user);
     var token = user.token
    //  console.log(token)
    var shenhe = user.shenhe
    wx.request({
      url: that.data.getmyUrl,
      method: 'post',
      data: { token: token, shenhe: shenhe },
      success: function (res) {
        var data = res.data;
        console.log(data)
        var shuju = data.data
        if (data.code = 200) {
          that.setData({ shuju: shuju });
        } else {
          wx.showToast({
            title: '获取失败',
            icon: 'none',
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    var campus_id = that.data.campus_id;
    var xueduan = that.data.xueduan;
    var kemu = that.data.kemu;
    // console.log(xiaoqu)
    wx.request({
      url: that.data.getxiaoquUrl,
      method: 'post',
      success: function (res) {
        var data = res.data;
        // console.log(data)
        if (data.code = 200) {
          var items = res.data.data;
          that.setData({ items: items })
        }
      }
    })
    wx.request({
      url: that.data.getxueduanUrl,
      method: 'post',
      success: function (res) {
        var data = res.data;
        // console.log(data)
        if (data.code = 200) {
          var xueduan = res.data.data;
          that.setData({ xueduan: xueduan })
        }
      }
    })
    wx.request({
      url: that.data.getkemuUrl,
      method: 'post',
      success: function (res) {
        var data = res.data;
        // console.log(data)
        if (data.code = 200) {
          var kemu = res.data.data;
          that.setData({ kemu: kemu })
        }
      }
    })
    wx.request({
      url: that.data.getzhiweiUrl,
      method: 'post',
      success: function (res) {
        var data = res.data;
        // console.log(data)
        if (data.code = 200) {
          var zhiwei = res.data.data;
          that.setData({ zhiwei: zhiwei })
        }
      }
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})